<?php

/**
 * Created by 深圳市丹宏昊天电子商务有限公司.
 * 作 者: sqingwen
 * 日 期: 2016/12/26
 *
 */
class MdyOrderAddress extends CI_Controller
{
    private $t_order = 'mt_order';  //订单表

    function __construct()
    {
        parent::__construct();
        $this->load->model('api_model/tr_order_model' , 'trOrderModel');
    }

    /**
     *
     */
    public function index()
    {
        $gpData = gp();
        $logInfo = array('file_path' => __FILE__ , 'fun' => __FUNCTION__ , 'data' => $gpData);
        log_output($logInfo);
        $res = 0;
        $orderNum = $gpData['order_num'];
        $sql = sprintf("select `id` from `mt_product` where `order_num` = '%s'" , $orderNum);


        if (empty($orderNum)) {
            echo('-1');
            exit;
        }
        $product = $this->db->query($sql)->result_array();


        if (empty($product)) {
            echo('-2');
            exit;
        }

        if (empty($gpData['country']) || empty($gpData['city']) || empty($gpData['state']) || empty($gpData['street'])) {
            echo('-3');//参数不完整
            exit;
        }

        $sql_ck_country = "select `iso` from `mt_country` where iso='" . $gpData['country'] . "' or `nombre`='" . $gpData['country'] . "' or `name_en`='" . $gpData['country'] . "' or `name_ru`='" . $gpData['country'] . "' or `name_de`='" . $gpData['country'] . "' ";
        $res_country = $this->db->query($sql_ck_country)->row_array();

        if (empty($res_country)) {
            echo('-4');//国家不符合规范
            exit();
        }

        $data = array('delivery_country' => $res_country['iso'] ,//国家必须符合iso规范
            'delivery_city' => $gpData['city'] ,//城市
            'delivery_state' => $gpData['state'] ,//州
            'delivery_street_address' => $gpData['street'] ,//街道
            'delivery_zipcode' => $gpData['zipcode'] ,//邮编
            'delivery_address' => $gpData['zipcode'] . ' ' . $gpData['street'] . ' ' . $gpData['state'] . ' ' . $gpData['city'] . ' ' . $gpData['country']

        );

        $this->db->where('order_num' , $orderNum);
        $res_mdy = $this->db->update($this->t_order , $data);


        if ($res_mdy == 1) {
            //修改运费

            $this->load->model('fee_model' , 'feeModel');
            foreach ($product as $k => $v) {
                $id = intval($v['id']);
                $res = $this->feeModel->setProductPrice($id);//这个id是产品表的id
            }

            //if ($res) {
            echo('1');//修改成功
            exit();
            // }
            // else {
            //    echo('-5');//修改成功，但是修改费用失败
            //     exit;
            // }
        }
        else {
            echo('-6');//修改地址失败
            exit;
        }


        exit;
    }
}